"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : DATABASE
Case Name   : A兼容模式下使用array_to_string函数查询
Create At   : 2022/11/18
Owner       : opentestcase004
Description :
    1.创建ORACLE兼容模式数据库
    2.使用array_to_string函数查询
    3.清理环境
Expect      :
    1.成功
    2.返回空
    3.成功
History     :
"""
import os
import unittest
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Auditing(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.database = 'd_test_0017'

    def test_security(self):
        text = '-----step1:创建ORACLE兼容模式数据库 ;expect:成功-----'
        self.logger.info(text)
        sql_cmd = f"drop database if exists {self.database};" \
            f"create database {self.database} dbcompatibility='A';"
        self.logger.info(sql_cmd)
        result = self.sh_primy.execut_db_sql(sql_cmd)
        self.logger.info(result)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, result, '执行失败'
                      + text)

        text = '-----step2:使用array_to_string函数查询;expect:返回空-----'
        self.logger.info(text)
        sql_cmd = f"select array_to_string(array[1,0,1,0], '');"
        self.logger.info(sql_cmd)
        result = self.sh_primy.execut_db_sql(sql_cmd, dbname=self.database)
        self.logger.info(result)
        self.assertEqual(" ", str(result.splitlines()[-2]), text + '执行失败')

    def tearDown(self):
        text = '-----step3:清理环境;expect:成功-----'
        self.logger.info(text)
        clean_cmd = f"drop database {self.database};"
        self.logger.info(clean_cmd)
        clean_msg = self.sh_primy.execut_db_sql(clean_cmd)
        self.logger.info(clean_msg)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, clean_msg,
                      '执行失败' + text)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
